#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.exception.common_exception import ErrCodeException
from mysql import log
from mysql.src.common.constant import MySQLStrConstant, SubJobType, SubJobPolicy
from mysql.src.common.error_code import MySQLErrorCode
from mysql.src.common.execute_cmd import is_port_in_use
from mysql.src.protect_mysql_base_utils import check_tool_exist
from mysql.src.service.base_service import BaseService
from mysql.src.service.restore.restore_param import RestoreParam
from mysql.src.utils.common_func import get_backup_tool_name
from mysql.src.utils.service_func import get_service_info


class Restore(BaseService):
    def __init__(self, job_id, sub_job_id, restore_param: RestoreParam):
        super().__init__(job_id, sub_job_id, restore_param.pid)
        self.param: RestoreParam = restore_param

    def check_inst_status(self):
        if self.param.is_pxc_master:
            is_running, _, _ = get_service_info(MySQLStrConstant.MYSQLPXCSERVICES)
            if is_running:
                log.error(f"{MySQLStrConstant.MYSQLPXCSERVICES} is running,{self.get_log_comm()}")
                raise ErrCodeException(MySQLErrorCode.CHECK_MYSQL_NOT_CLOSE)
        elif self.param.is_eapp_node:
            is_running, _, _ = get_service_info(MySQLStrConstant.EAPPMYSQLSERVICES)
            if is_running:
                log.error(f"{MySQLStrConstant.EAPPMYSQLSERVICES} is running,{self.get_log_comm()}")
                raise ErrCodeException(MySQLErrorCode.CHECK_MYSQL_NOT_CLOSE)
        elif self.param.is_pxc_node:
            is_running, _, _ = get_service_info(MySQLStrConstant.MYSQLPXCSERVICES)
            if is_running:
                log.error(f"mysql service is in running,{self.get_log_comm()}")
                raise ErrCodeException(MySQLErrorCode.CHECK_MYSQL_NOT_CLOSE)
        else:
            if is_port_in_use(self.param.port):
                log.error(f"port:{self.param.port} is in use:{self.get_log_comm()}")
                raise ErrCodeException(MySQLErrorCode.CHECK_MYSQL_NOT_CLOSE)

    def check_tool_path(self):
        if self.param.tool_path:
            tool_path = self.param.tool_path
        else:
            tool_path = get_backup_tool_name(self.param.version)
        ret = check_tool_exist(tool_path, self.param.ld_library_path)
        if not ret:
            log.error(f"backup tool not found")
            raise ErrCodeException(MySQLErrorCode.CHECK_BACKUP_TOOL_FAILED)

    def restore_prerequisite(self):
        log.info(f"start restore prerequisite:{self.get_log_comm()}")
        self.check_inst_status()
        self.check_tool_path()

    def gen_sub_job(self):
        pass

    def restore(self):
        pass

    def build_sub_job(self, job_priority, exec_node_id, job_info, job_name):
        sub_job_info = dict()
        sub_job_info["jobId"] = self.job_id
        sub_job_info["subJobId"] = ""
        sub_job_info["jobType"] = SubJobType.BUSINESS_SUB_JOB.value
        sub_job_info["jobName"] = job_name
        sub_job_info["jobPriority"] = job_priority
        sub_job_info["policy"] = SubJobPolicy.FIXED_NODE.value
        sub_job_info["ignoreFailed"] = False
        sub_job_info["execNodeId"] = exec_node_id
        sub_job_info["jobInfo"] = job_info
        return sub_job_info
